<template>
	<view class="subsystem-work-detail-submit-main">
		<textarea class="textarea" v-model="inputData" placeholder="请输入140个字以内的内容" />

		<view class="img-content">
			<view v-for="(item,index) in imgPathList" :key="index">
				<image class="uploadImg mr20" :src="item" mode="aspectFit|aspectFill|widthFix" lazy-load="false">
				</image>
			</view>
			<!-- 图片小于3张的时候显示添加图片按钮,禁用状态不显示添加按钮 -->
			<image @click="tapchoosePhoto(index)" v-if="!imgPathList || imgPathList.length < 3" class="uploadImg mr20"
				src="https://fireware-1257276602.cos.ap-guangzhou.myqcloud.com/bus/increase02.png" mode="aspectFit|aspectFill|widthFix" lazy-load="false"></image>
		</view>

		<view class="submit" @click="submitHandle">
			提交
		</view>
	</view>
</template>

<script>
	import {
		baseUrl,
		request
	} from "../../request/index.js"
	export default {
		data() {
			return {
				inputData: "",
				imgPathList: [],
				uploadImgPathList: [],
				query: {},
				bj_img: 'https://fireware-1257276602.cos.ap-guangzhou.myqcloud.com/fsw-wx-lmini/icons/uploadImg.png',
			}
		},
		onLoad(options) {
			this.query = JSON.parse(options.query)
		},
		methods: {
			tapchoosePhoto(e) {
				const that = this
				uni.chooseMedia({
					count: 3,
					mediaType: ['image'],
					sizeType: ['original', 'compressed'],
					sourceType: ['album', 'camera'],
					camera: 'back',
					success(res) {
						that.btndetermine(res.tempFiles)
					}
				})
			},
			//图片上传
			async btndetermine(paths) {
				console.log('paths', paths)
				if (paths.length === 0) {
					uni.showToast({
						title: '请先选择图片'
					})
					return
				}
				let images = []
				let that = this
				for (var i = 0; i < paths.length; i++) {
					uni.uploadFile({
						url: `${baseUrl}api/oct/wemini/file/upload`,
						header: {
							'Content-Type': 'application/x-www-form-urlencoded',
							openid: uni.getStorageSync("openid") || 'oRekA4wN8O7hyfAlgdRNl4ughrm4'
						},
						filePath: paths[i].tempFilePath, // 文件临时路径；
						name: 'file', // 文件名，可以随便写
						formData: {
							type: 'image',
							model: 'task'
						},
						success: function(res) {
							if (res.statusCode === 200) {
								let data = JSON.parse(res.data)
								images.push(data.payload)
							}
							if (images.length === paths.length) {
								that.imgPathList = [...that.imgPathList, ...paths.map(item =>
									item.tempFilePath)]
								that.uploadImgPathList = [...that.uploadImgPathList, ...images]
							}
						}
					})
				}
			},
			async submitHandle() {
				//  处理
				const res = await request({
					url: "api/oct/wemini/bus/workOrder/handleWorkOrder",
					method: "POST",
					data: {
						...this.query,
						actionContent: this.inputData,
						actionimgPaths: this.uploadImgPathList,
					}
				})
				if (res.status == 0) {
					let pages = getCurrentPages()
					let beforePage = pages[pages.length - 2]

					setTimeout(async () => {
						await beforePage.$vm.getWorkDetailHandle()
						beforePage.$vm.getButtonPermission()

						uni.navigateBack({
							delta: 1
						});
					}, 1000)
				} else {
					uni.showToast({
						title: '处理失败!',
						icon:"none",
						duration: 2000
					})
				}
			},
		}
	}
</script>

<style lang="less">
	.subsystem-work-detail-submit-main {
		padding: 20rpx;

		.img-content {
			display: flex;
		}

		.textarea {
			padding: 10rpx;
			width: 90vw;
			height: 200rpx;
			background-color: #fff;
			border-radius: 10rpx;
		}

		.submit {
			position: fixed;
			left: 0;
			bottom: 0;
			background-color: #6892fa;
			width: 100vw;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			color: #fff;
		}
	}
</style>